<?
if(!defined ('U_MOD'))
{
	echo"<meta http-equiv='refresh' content='0;url=/index.php'>";
	exit;
}

function getLogo()
{
	global $portal_url, $portal_title;
	print'<a href="'.$portal_url.'">'.$portal_title.'</a>';
}

function getSlogan()
{
	global $portal_slogan;
	print $portal_slogan;
}

//*************************
function getMenuArray($res)
{
	$hLevels = array(); $vLevels = array();
	$hTree = array(); $vTree = array();
	$hCur = array(); $vCur = array();

	$menu=array();

	while($rows = mysql_fetch_assoc($res))
	{
		if ($rows['nav_page_position']=='both' || $rows['nav_page_position']=='h')
		{
			$hCur = &$hLevels[$rows['nav_id']];
			$hCur['nav_id'] = $rows['nav_id'];
			$hCur['nav_parent_id'] = $rows['nav_parent_id'];
			$hCur['nav_admin'] = $rows['nav_admin'];
			$hCur['nav_module'] = $rows['nav_module'];
			$hCur['nav_page'] = $rows['nav_page'];
			$hCur['nav_title'] = pEcho($rows['nav_title']);

			if($rows['nav_parent_id'] == 0)
				$hTree[$rows['nav_id']] = &$hCur;
	        else
				$hLevels[$rows['nav_parent_id']]['children'][$rows['nav_id']] = &$hCur;
		}
		if ($rows['nav_page_position']=='both' || $rows['nav_page_position']=='v')
		{
			$vCur = &$vLevels[$rows['nav_id']];
			$vCur['nav_id'] = $rows['nav_id'];
			$vCur['nav_parent_id'] = $rows['nav_parent_id'];
			$vCur['nav_admin'] = $rows['nav_admin'];
			$vCur['nav_module'] = $rows['nav_module'];
			$vCur['nav_page'] = $rows['nav_page'];
			$vCur['nav_title'] = pEcho($rows['nav_title']);

			if($rows['nav_parent_id'] == 0)
				$vTree[$rows['nav_id']] = &$vCur;
        	else
				$vLevels[$rows['nav_parent_id']]['children'][$rows['nav_id']] = &$vCur;
		}
    }

	$menu['hMenu']=$hTree;

	$menu['vMenu']=$vTree;

	return $menu;
}

//***************************

function GetActivePage($arr, $CurrentModule, $shift, $parent)
{
	global $u_rule;
	$functionResult='';

	foreach($arr as $k1=>$v1)
	{
		if($CurrentModule=='admin')
		{
			if ($v1['nav_module']=='portal' && $v1['nav_admin']==1)
			{
				$functionResult=$v1['nav_parent_id'];
				return $functionResult;
			}else
			{
				if (!empty($v1['children']))
				{
					$shift++;
					$functionResult=GetActivePage($v1['children'], $CurrentModule, $shift, $v1['nav_parent_id']);
				}
			}
		}elseif($CurrentModule=='user')
		{
			if ($v1['nav_module']=='portal' && $v1['nav_admin']==0)
			{
				$functionResult=$v1['nav_parent_id'];
				return $functionResult;
			}else
			{
				if (!empty($v1['children']))
				{
					$shift++;
					$functionResult=GetActivePage($v1['children'], $CurrentModule, $shift, $v1['nav_parent_id']);
				}
			}
		}else
		{
			if($v1['nav_module']==$CurrentModule)
			{
				$functionResult=$v1['nav_parent_id'];
				return $functionResult;
			}else
			{
				if (!empty($v1['children']))
				{
					$shift++;
					$functionResult=GetActivePage($v1['children'], $CurrentModule, $shift, $v1['nav_parent_id']);
				}
			}
		}
	}

	if ($functionResult!='' && $shift!=0)
	{
			$AllMenu=getMenu($u_rule);
			$functionResult=GetParentArray($AllMenu['vMenu'], $functionResult);
			return $functionResult;
	}
}

function GetParentArray($arr, $parent)
{
	$functionResult='';
	foreach($arr as $k1=>$v1)
	{
		if (!empty($v1['children']))
		{
			if(isset($v1['children'][$parent]) && !empty($v1['children'][$parent]))
			{
				$functionResult=$v1['nav_id'];
				break;
			}else
				$functionResult=GetParentArray($v1['children'], $parent);
		}
	}
	return $functionResult;
}

function getMenuTree($arr, $child, $activity, $CurrentModule)
{
	global $umk, $upk, $amk, $apk, $portal_url;

	if ($activity=='active')
		$ulClasAdd='class="active"';
	else
		$ulClasAdd='';

	$out = '';

	if ($child==1)
		$out .= '<ul '.$ulClasAdd.'>';

	foreach($arr as $k=>$v)
	{
		if($CurrentModule=='admin')
		{
			if ($v['nav_module']=='portal' && $v['nav_admin']==1)
				$urlClassAdd='active';
			else
				$urlClassAdd='';
		}elseif($CurrentModule=='user')
		{
			if ($v['nav_module']=='portal' && $v['nav_admin']!=1)
				$urlClassAdd='active';
			else
				$urlClassAdd='';
		}else
		{
			if($v['nav_module']==$CurrentModule)
				$urlClassAdd='active';
			else
				$urlClassAdd='';
		}

		$out .= '<li>'.CreateLink($portal_url, $v['nav_admin'], $v['nav_module'], $v['nav_page'], $v['nav_title'], '', 'var', array('',$urlClassAdd,'','',''));

		if(!empty($v['children']))
		{
			$activeChildPage=GetActivePage($v['children'], $CurrentModule, 0, 0);

			if($activeChildPage==$v['nav_id'])
				$ulClassAdd='active';
			else
				$ulClassAdd='';

			if($urlClassAdd=='active' || $ulClassAdd=='active')
				$activity='active';
			else
				$activity='';

			$out .= getMenuTree($v['children'], 1, $activity, $CurrentModule);
		}
		else
			$out .= '</li>';
	}

	if ($child==1)
		$out .= '</ul></li>';

	return $out;
}
//***************************

function getMenu($u_rule)
{
	global $PortalPref, $CURRENT_MODULE;
	$queryAdd="OR nav_mkey='".$CURRENT_MODULE."'";

	$query=mysql_query("
	SELECT
		*
	FROM
		".$PortalPref."navigation
	WHERE
		(nav_mkey='portal' ".$queryAdd.")
	AND
		nav_right>='".$u_rule."'
	AND
		nav_activity='1'
	ORDER BY
		nav_order
	ASC
");

	$menuArray = getMenuArray($query);
    return $menuArray;
}
//******************************

function getSideBlockArray()
{
	global $PortalPref, $u_rule, $CURRENT_MODULE;
	$sideBlocksArray=array();

	$blockQueryAdd="OR blocks_mkey='".$CURRENT_MODULE."'";

	$blocksQuery=mysql_query("
		SELECT
			*
		FROM
			".$PortalPref."blocks
		WHERE
			(blocks_h_position='left'
		OR
			blocks_h_position='right')
		AND
			(blocks_mkey='portal' ".$blockQueryAdd.")
		AND
			blocks_activity='1'
		AND
			blocks_rights>='".$u_rule."'
		ORDER BY
			blocks_v_position
		ASC
	");

	$l=0; $r=0;
	while($result=mysql_fetch_assoc($blocksQuery))
	{
		if ($result['blocks_h_position']=='left')
		{
			$sideBlocksArray['left'][$l]['title']=$result['blocks_title'];
			$sideBlocksArray['left'][$l]['content']=$result['blocks_content'];
			$sideBlocksArray['left'][$l]['metka']=$result['blocks_metka'];
			$l++;
		}else
		{
			$sideBlocksArray['right'][$r]['title']=$result['blocks_title'];
			$sideBlocksArray['right'][$r]['content']=$result['blocks_content'];
			$sideBlocksArray['right'][$r]['metka']=$result['blocks_metka'];
			$r++;
		}
	}

	return $sideBlocksArray;
}

function getSideBlock($side, $array)
{
	global $METKA;
	$i=0;
	for ($i=0; $i<count($array[$side]); $i++)
	{
		getSideBlockTheme('begin');
			getSideBlockTheme('header');
					if (defined($array[$side][$i]['title']))
						print constant($array[$side][$i]['title']);
					else
						print $array[$side][$i]['title'];
				getSideBlockTheme('header_end');
			endDiv();
			getSideBlockTheme('content');
				if ($array[$side][$i]['metka']==1)
					eval($METKA[$array[$side][$i]['content']]);
				else
					print '<p>'.$array[$side][$i]['content'].'</p>';
			endDiv();
		endDiv();
	}
}

function getVMenu($vMenuArray, $CurrentModule)
{
	getVMenuTheme('begin');
		getVMenuTheme('header');
				print _NAVIGATION_;
			getVMenuTheme('header_end');
		endDiv();
		getVMenuTheme('content');
				print getMenuTree($vMenuArray, 0, '', $CurrentModule);
				/*
				print '<li><a href="new-page.html" class="active">Page 1</a>
						<ul class="active">
							<li><a href="new-page/new-page.html">Subpage 1</a></li>
							<li><a href="new-page/new-page-2.html">Subpage 2</a></li>
							<li><a href="new-page/new-page-3.html">Subpage 3</a></li>
						</ul>
					</li>
					<li><a href="new-page-2.html">Page 2</a></li>';*/
			getVMenuTheme('content_end');
		endDiv();
	endDiv();
}

function getPageName()
{
	print'Page 1';
}

function getCentralBlockArray()
{
	global $PortalPref, $u_rule, $CURRENT_MODULE;
	$centralBlocksArray=array();

	$blockQueryAdd="OR blocks_mkey='".$CURRENT_MODULE."'";

	$blockSQuery=mysql_query("
		SELECT
			*
		FROM
			".$PortalPref."blocks
		WHERE
			blocks_h_position='central'
		AND
			(blocks_mkey='portal' ".$blockQueryAdd.")
		AND
			blocks_activity='1'
		AND
			blocks_rights>='".$u_rule."'
		ORDER BY
			blocks_v_position
		ASC
	");

	$i=0;
	while($result=mysql_fetch_assoc($blockSQuery))
	{
		$centralBlocksArray[$result['blocks_type']][$i]['content']=$result['blocks_content'];
		$centralBlocksArray[$result['blocks_type']][$i]['metka']=$result['blocks_metka'];
		$i++;
	}

	return $centralBlocksArray;
}

function getCentralBlock($type, $centralBlocksArray)
{
	global $METKA;
	if ($type==0)
	{
		foreach ($centralBlocksArray as $key => $value)
		{
			getHeaderTheme('banner_begin');
				if ($centralBlocksArray[$key]['metka']==1)
					eval($METKA[$centralBlocksArray[$key]['content']]);
				else
					print $centralBlocksArray[$key]['content'];
			getHeaderTheme('banner_end');
		}
	}elseif ($type==1)
	{
		foreach ($centralBlocksArray as $key => $value)
		{
			getCentralRow('begin');
				getCentralBlockTheme($type);
					if ($centralBlocksArray[$key]['metka']==1)
					{
						print '<p style="text-align: center;">';
							eval($METKA[$centralBlocksArray[$key]['content']]);
						print '</p>';
					}else
						print '<p style="text-align: center;">'.$centralBlocksArray[$key]['content'].'</p>';
				endDiv();
			getCentralRow('end');
		}
	}elseif($type==2)
	{
		foreach ($centralBlocksArray as $key => $value)
		{
			getCentralBlockTheme($type);
				if ($centralBlocksArray[$key]['metka']==1)
				{
					print '<p style="text-align: justify;">';
						eval($METKA[$centralBlocksArray[$key]['content']]);
					print '</p>';
				}else
					print '<p style="text-align: justify;">'.$centralBlocksArray[$key]['content'].'</p>';
			endDiv();
		}
	}elseif($type==3)
	{
		foreach ($centralBlocksArray as $key => $value)
		{
			getCentralBlockTheme($type);
				if ($centralBlocksArray[$key]['metka']==1)
				{
					print '<p style="text-align: center;">';
						eval($METKA[$centralBlocksArray[$key]['content']]);
					print '</p>';
				}else
					print '<p style="text-align: center;">'.$centralBlocksArray[$key]['content'].'</p>';
			endDiv();
		}
	}elseif($type==4)
	{
		foreach ($centralBlocksArray as $key => $value)
		{
			getCentralBlockTheme($type);
				if ($centralBlocksArray[$key]['metka']==1)
				{
					print '<p style="text-align: center;">';
						eval($METKA[$centralBlocksArray[$key]['content']]);
					print '</p>';
				}else
					print '<p style="text-align: center;">'.$centralBlocksArray[$key]['content'].'</p>';
			endDiv();
		}
	}
}

function getRss()
{
	print'<a href="#" title="RSS" class="lv-rss-tag-icon" style="position:absolute;bottom:8px;left:6px"></a>';
}

function getFooter($showMenu, $showCopy)
{
	global $metaArray, $PortalPref, $u_rule, $CURRENT_MODULE, $portal_url;

	if ($showMenu==1)
	{
		$queryAdd="OR nav_mkey='".$CURRENT_MODULE."'";
		$queryNavFooter=mysql_query("
			SELECT
				*
			FROM
				".$PortalPref."navigation
			WHERE
				(nav_mkey='portal' ".$queryAdd.")
			AND
				nav_right>='".$u_rule."'
			AND
				nav_activity='1'
			AND
				nav_footer>'0'
			ORDER BY
				nav_footer
			ASC
		");

		if (mysql_num_rows($queryNavFooter)>0)
		{
			$footerNav='';
			while($resultNavFooter=mysql_fetch_assoc($queryNavFooter))
			{
				 $footerNav.=CreateLink($portal_url, $v['nav_admin'], $resultNavFooter['nav_module'], $resultNavFooter['nav_page'], pEcho($resultNavFooter['nav_title']), '', 'var', array('','','','',''))." | ";
			}
			$footerNav=substr($footerNav, 0, -3);
			print '<p>'.$footerNav.'</p>';
		}
	}

	if ($showCopy==1)
		print'<p>'.getCopyRightSymbol($metaArray['_META_PORTAL_COPYRIGHT_']).'</p>';
}

function getCopyRight()
{
	print 'Designed by <a href="lavina.dp.ua" target="_blank">Eugene</a>.';
}
?>

<!--
<!--BLOCK>
						<div class="lv-block clearfix">
							<div class="lv-blockheader">
								<h3 class="t">Поиск</h3>
							</div>
							<div class="lv-blockcontent">
								<div>
									<form action="#" class="lv-search" method="get" name="searchform">
										<input type="text" value="" name="s" />
										<input type="submit" value="Search" name="search" class="lv-search-button" />
									</form>
								</div>
							</div>
						</div>
						<!--//BLOCK>

						<!--BLOCK>
						<div class="lv-block clearfix">
							<div class="lv-blockheader">
								<h3 class="t">Категории</h3>
							</div>
							<div class="lv-blockcontent">
								<div>
									<p>Lorem ipsum dolor sit amet. Nam sit amet sem. Mauris a ante.</p>
									<ul>
										<li><a href="#" title="All News">All News</a> (50)</li>
										<li><a href="#" title="Best of the Year">Best of the Year</a> (4)</li>
										<li><a href="#" title="Hyperlink">Hyperlink</a> (24)</li>
										<li><a href="#" title="Visited link" class="visited">Visited link</a> (17)</li>
										<li><a href="#" title="Hovered link" class="hover">Hovered link</a> (6)</li>
									</ul>
								</div>
							</div>
						</div>
						<!--//BLOCK>

						<!--BLOCK>
						<div class="lv-block clearfix">
							<div class="lv-blockheader">
								<h3 class="t">Архив</h3>
							</div>
							<div class="lv-blockcontent">
								<div>
									<ul>
										<li><a href="#" title="November 2008">November 2008</a></li>
										<li><a href="#" title="October 2008">October 2008</a></li>
										<li><a href="#" title="September 2008">September 2008</a></li>
										<li><a href="#" title="June 2008">June 2008</a></li>
									</ul>
								</div>
							</div>
						</div>
						<!--//BLOCK>

						<!--BLOCK>
						<div class="lv-block clearfix">
							<div class="lv-blockheader">
								<h3 class="t">Блогролл</h3>
							</div>
							<div class="lv-blockcontent">
								<div>
									<ul>
										<li><a href="#">My first blog</a></li>
										<li><a href="#">Who will prevail?</a></li>
										<li><a href="#">Stay positive!</a></li>
										<li><a href="#">Oil still going up</a></li>
										<li><a href="#">Gripes and Grins</a></li>
										<li><a href="#">2012 Olympics</a></li>
										<li><a href="#">How to SEO</a></li>
										<li><a href="#">On Global Warming</a></li>
										<li><a href="#">Can you guess?</a></li>
									</ul>
								</div>
							</div>
						</div>
						<!--//BLOCK>
		-->
		<!--<p>
												<img src="images/preview.jpg" alt="an image" id="preview-image" name="preview-image" />
												Enter Page content here...
											</p>
											<p>
												Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam pharetra, tellus
												sit amet congue vulputate, nisi erat iaculis nibh, vitae feugiat sapien ante
												eget mauris.&nbsp;Aenean sollicitudin imperdiet arcu, vitae dignissim est posuere id.
											</p>
											<p><a href="#">Read more</a></p>
											-->


											<!--
											<p>
				<ul>
					<li>Suspendisse pharetra auctor pharetra. Nunc a sollicitudin est.</li>
					<li>Donec vel neque in neque porta venenatis sed sit amet lectus.</li>
					<li>Curabitur ullamcorper gravida felis, sit amet scelerisque lorem iaculis sed.</li>
				</ul>
			</p>
			-->

			<!--
			<div class="lv-layout-cell layout-item-0" style="width: 50%" >
										<p>
											<blockquote style="margin: 10px 0">
												Nunc a sollicitudin est. Curabitur ullamcorper gravida felis, sit amet scelerisque
											 	lorem iaculis sed. Donec vel neque in neque porta venenatis sed sit amet lectus.
											</blockquote>
										</p>
									</div>
			-->